{
capture drop _merge
sort b_id year
merge m:1 b_id year using  "${source_data}\Downstream_Data.dta"
drop if _merge==2

label var D_sourcing "\$Relational^D_b\$" 
label var S_sourcing "\$Relational_b\$" 

capture drop sample
gen sample=(n_ud==1 & S_pds==1)

tab _merge if sample==1

corr sales_down ln_q_b_all if n_bt==1  
reghdfe ln_q_b_all sales_down if n_bt==1, abs(top_dest_b year) 

egen gr_sdtilde=group(s_id destination_order)
egen gr_djt=group(top_dest_b gr_jt)
egen gr_dtildejt=group(gr_jt destination_order)

label var sales_down "\$Downstream_{by}\$"

capture drop sample
gen sample=(n_ud==1 & S_pds==1)

gen eu_sample=(sample==1 & sales_down!=.)
bys b_id: egen eu_b_in_sample=max(eu_sample)

local VAR sourcing_metric_b_excl 
foreach j of local VAR{

capture drop EU_`j'	
capture drop xx
capture drop mean_xx
capture drop sd_xx
gen xx=`j' if n_b==1 & eu_b_in_sample==1
egen mean_xx=mean(xx)
egen sd_xx=sd(xx)
gen EU_`j'=(`j'-mean_xx)/sd_xx

replace EU_`j'=. if eu_b_in_sample==0

}

label var EU_sourcing_metric_b_excl "\$Relational_b\$"

capture drop sample
gen sample=(n_ud==1 & S_pds==1)

eststo clear
* Baseline 
eststo: reghdfe ln_mu_o_B   S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month    ln_q_sbjo ln_p_inpf_sbjo  ///
if sample==1, ///
abs(gr_sjt top_dest_b cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
    quietly estadd local CTRL2 =".", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjt,d", replace  
  quietly estadd scalar  U =  . , replace  

* Destination-Product-Year effects
eststo: reghdfe ln_mu_o_B   S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month    ln_q_sbjo ln_p_inpf_sbjo  ///
if sample==1, ///
abs(gr_sjt gr_djt cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
    quietly estadd local CTRL2 =".", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjt,djt", replace  
  quietly estadd scalar  U =  . , replace  

* seller-destination of the buyer fixed effects 
 eststo: reghdfe ln_mu_o_B   S_sourcing   ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month    ln_q_sbjo ln_p_inpf_sbjo  ///
if sample==1, ///
abs(gr_sjt gr_sdtilde  cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
   quietly estadd local CTRL2 =".", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjt,sd", replace  
  quietly estadd scalar  U =  . , replace     
  
* Baseline (rich) regression, where destination is the order, not the buyer
eststo: reghdfe ln_mu_o_B   S_sourcing  ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month    ln_q_sbjo ln_p_inpf_sbjo  ///
if sample==1, ///
abs(gr_sjt gr_dtildejt  cohort_sb cohort_b) vce (cluster b_id)
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
    quietly estadd local CTRL2 =".", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjt,cjt", replace  
  quietly estadd scalar  U =  . , replace  
  
* Baseline in subsample for which we have size of the buyer downstream (with the control)
eststo: reghdfe ln_mu_o_B   EU_sourcing_metric_b_excl sales_down   ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month    ln_q_sbjo ln_p_inpf_sbjo  ///
if sample==1, ///
abs(gr_sjt top_dest_b cohort_sb cohort_b) vce (cluster b_id) 
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local CTRL2 =".", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjt,d", replace  
  quietly estadd scalar  U =  . , replace  

capture drop temp_sample
gen temp_sample=e(sample)

* Baseline in subsample for which we have size of the buyer downstream (without the control)
eststo: reghdfe ln_mu_o_B   EU_sourcing_metric_b_excl    ln_q_sb_all sh_s_b sh_b_s age_buysel_month signatory  ///
 ln_q_b_all age_buy_month    ln_q_sbjo ln_p_inpf_sbjo  ///
if temp_sample==1, ///
abs(gr_sjt top_dest_b cohort_sb cohort_b) vce (cluster b_id) 
  quietly estadd local CTRL ="\scriptsize{B,R,O}", replace  
  quietly estadd local CTRL2 =".", replace  
  quietly estadd local est ="OLS", replace  
  quietly estadd local FE ="sjt,d", replace  
  quietly estadd scalar  U =  . , replace  

esttab using  "${tables}\TableD6.tex", ///
  label replace booktabs b(3) se(3) noconstant obslast substitute(\_ _) ///
      title("The Downstream Market" \label{Downstream}) ///
  drop(ln_q_sb_all sh_s_b sh_b_s age_buysel_months ln_q_b_all age_buy_months  signatory ln_q_sbjo ln_p_inpf_sbjo  ///
     _cons ) order(    ) /// 
  stats(FE CTRL  r2   N   , fmt( %~#s %~#s   2 %12.0fc  )  ///
  labels("FEs" "Controls"    `"\(R^{2}\)"'  "Obs."  ))   ///
  star(* 0.10 ** 0.05 *** 0.01) ///
  nonotes   

} 
